// 依赖包
const express = require('express');
const bodyParser = require('body-parser');
const consolidate = require('consolidate');
const ejs=require('ejs'); 
const mysql = require('mysql');
const cookieSession = require('cookie-session');
const redis = require('redis');

// 配置文件
const config = require('./config');


// 搭建服务器
let server = express();
server.listen(config.port)

const db = mysql.createPool({host:config.mysql_host,port:config.mysql_port,user:config.mysql_user,password:config.mysql_password,database:config.mysql_dbname});
const client = redis.createClient(config.RDS_PORT,config.RDS_HOST,config.RDS_OPTS);
client.auth(config.RDS_PWD);
client.on('ready',function(res){
    console.log('ready');    
});
server.use((req,res,next)=>{
    req.db = db;  
    req.client = client; 
    next();
});

// 中间件
// 普通的POST数据
server.use(bodyParser.urlencoded({extended:false}));
// session
server.use(cookieSession({keys:require('./secrecy/sess_key')}));

// 模版
server.set('html',consolidate.ejs);
server.set('view engine','ejs');
server.set('views','./template');


// 静态资源
server.use(express.static('./www/'));

// 处理请求
server.use('/',require('./routers/www'));









